#ifndef COMMUNICATIONLOGGER_H
#define COMMUNICATIONLOGGER_H

#include <QAbstractListModel>
#include <QDateTime>
#include <QList>

class CommunicationLogger : public QAbstractListModel
{
    Q_OBJECT
    
public:
    enum LogDirection { TX, RX, ERROR, INFO };
    
    enum LogRoles {
        TimestampRole = Qt::UserRole + 1,
        DirectionRole,
        MessageRole
    };
    
    explicit CommunicationLogger(QObject *parent = nullptr);
    
    Q_INVOKABLE void clear();
    Q_INVOKABLE void saveToFile(const QString &filePath);
    
    void logFrame(LogDirection direction, const QByteArray &data);
    void logError(const QString &error);
    void logInfo(const QString &info);
    
    // QAbstractListModel interface
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;
    
private:
    struct LogEntry {
        QDateTime timestamp;
        LogDirection direction;
        QString message;
    };
    
    QList<LogEntry> m_logs;
    QString formatFrame(const QByteArray &data);
    
    static constexpr int MAX_LOG_ENTRIES = 1000;
};

#endif // COMMUNICATIONLOGGER_H
